<?php

namespace hiduoke\services\douyinMinApp\API;

use GuzzleHttp\Client;

class BaseApi
{

    public function httpPost($url, $data, $headers = [])
    {
        $jsonBody = json_encode($data);
        if ($jsonBody === '[]') {
            $jsonBody = '{}';
        }
        $httpClient = new Client();
        $res = $httpClient->post($url, [
            'headers' => array_merge($headers, ['Content-Type' => 'application/json']),
            'body'    => $jsonBody
        ]);
        $res = $res->getBody()->getContents();
        $res = json_decode($res, true);
        return $res;
    }

    public function httpGet($url, $headers = [])
    {
        $httpClient = new Client();
        $res = $httpClient->get($url, [
            'headers' => array_merge($headers, ['Content-Type' => 'application/json']),
        ]);
        $res = $res->getBody()->getContents();
        $res = json_decode($res, true);
        return $res;
    }
}
